﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class UIBSSATZ : ViewBase
    {
        #region ==WTKEY===========
        public static readonly DependencyProperty WTKEYProperty = DependencyProperty.Register("WTKEY",
        typeof(string), typeof(UIBSSATZ),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWTKEYChanged));
        public string WTKEY
        {
            get
            {
                return (string)GetValue(WTKEYProperty);
            }
            set
            {
                SetValue(WTKEYProperty, value);
                OnWTKEYChg();
            }
        }
        private static void OnWTKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBSSATZ;
            if (src != null)
            {
                src.WTKEY = (string)e.NewValue;
            }
        }
        public virtual void OnWTKEYChg()
        { }
        #endregion

        #region ==FDZ===========
        public static readonly DependencyProperty FDZProperty = DependencyProperty.Register("FDZ",
        typeof(double), typeof(UIBSSATZ),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFDZChanged));
        public double FDZ
        {
            get
            {
                return (double)GetValue(FDZProperty);
            }
            set
            {
                SetValue(FDZProperty, value);
                
            }
        }
        private static void OnFDZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBSSATZ;
            if (src != null)
            {
                src.FDZ = (double)e.NewValue;
                src.OnFDZChg((double)e.NewValue);
            }
        }
        public virtual void OnFDZChg(double value)
        {
            if (MV == null)
                return;
            TMV.BSFDZ = value;
        }
        #endregion

        #region ==FAKTOR===========
        public static readonly DependencyProperty FAKTORProperty = DependencyProperty.Register("FAKTOR",
        typeof(double), typeof(UIBSSATZ),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFAKTORChanged));
        public double FAKTOR
        {
            get
            {
                return (double)GetValue(FAKTORProperty);
            }
            set
            {
                SetValue(FAKTORProperty, value);
                OnFAKTORChg();
            }
        }
        private static void OnFAKTORChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIBSSATZ;
            if (src != null)
            {
                src.FAKTOR = (double)e.NewValue;
            }
        }
        public virtual void OnFAKTORChg()
        { }
        #endregion
 

        public VBSSATZ TMV
        {
            get
            {
                return (VBSSATZ)MV;
            }
        }
        public UIBSSATZ()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            if (dwMaster == null)
            {
                WTKEY = "";
                KEY = "";
                FAKTOR = 0.0;
            }
            else
            {
                FDZ = TMV.BSFDZ;
                KEY ="B"+ TMV.BSKART;
                WTKEY = TMV.BSSRFNR;
                FAKTOR = TMV.BSFAKTOR;
            }
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (ORDERNUM.Length < 20)
                return;
            PM["ART"] = ORDERNUM.Substring(1, 20);             
            SResult = TMV.LOADBYART(PM);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }

        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateBSSATZ(dwMaster, 0);
            base.OnSave();
        }
    }
}
